package com.newcoin.chat.util;

import com.alibaba.fastjson.JSON;
import com.newcoin.chat.beans.ComparatorUser;
import com.newcoin.chat.beans.vo.UserVo;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import org.springframework.data.relational.core.sql.In;

import java.util.*;
import java.util.List;


public class PinYinUtil {

    public static String getFullSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else
                pybf.append(arr[i]);
        }
        return pybf.toString();
    }

    public static Map<String, Object> sort(List<UserVo> list) {
//        Comparator component = Collator.getInstance(Locale.CHINA);
        //按字母排序
        Collections.sort(list, new ComparatorUser());
        //输出26个字母
        Map<String, Object> map = new TreeMap<>();
        for (int i = 1; i <= 26; i++) {
            String word = String.valueOf((char) (96 + i)).toUpperCase();
            //循环找出 首字母一样的数据
            List<UserVo> letter = new ArrayList<>();
            for (UserVo str : list) {
                String zm = getFullSpell(str.getNickname()).substring(0, 1);
                if (word.equalsIgnoreCase(zm))
                    letter.add(str);
            }
            map.put(word, letter);
        }
        return map;
    }

    public static void main(String[] args) {
        List<UserVo> list = new ArrayList<>();
        UserVo userVo1 = new UserVo();
        userVo1.setId(1L);
        userVo1.setNickname("AliPay");
        userVo1.setHeadImgPath("1");

        UserVo userVo2 = new UserVo();
        userVo2.setId(2L);
        userVo2.setNickname("13644729399");
        userVo2.setHeadImgPath("2");

        UserVo userVo3 = new UserVo();
        userVo3.setId(3L);
        userVo3.setNickname("李三");
        userVo3.setHeadImgPath("3");

        UserVo userVo4 = new UserVo();
        userVo4.setId(4L);
        userVo4.setNickname("高六");
        userVo4.setHeadImgPath("4");

        UserVo userVo5 = new UserVo();
        userVo5.setId(5L);
        userVo5.setNickname("zzz");
        userVo5.setHeadImgPath("4");

        list.add(userVo1);
        list.add(userVo2);
        list.add(userVo3);
        list.add(userVo4);
        list.add(userVo5);

        System.out.println(JSON.toJSONString(sort(list)));

    }

}
